package com.red.wine.adapter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.red.wine.R;
import com.red.wine.model.TasteReturnData;
import com.red.wine.model.TasteInfo;
import com.red.wine.util.PrefsUtils;
import com.red.wine.util.StringUtil;


public class TasteAdapter extends BaseAdapter {
	private List<TasteInfo> datasource;
	private Context context;
	public TasteAdapter(Context context, List<TasteInfo> dataList) {
		this.context = context;
		this.datasource = dataList;
	}
	public int getCount() {
		return datasource.size();
	}

	public Object getItem(int position) {
		return datasource.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	 
	public View getView(int position, View convertView, ViewGroup parent) {
		Composites composites= null;  
		if( convertView == null ) {
               composites = new Composites();
               convertView = LayoutInflater.from(context).inflate(R.layout.taset_item_layout,null);
               composites.name = (TextView) convertView.findViewById(R.id.taset_item_title);
               composites.icon = (ImageView) convertView.findViewById(R.id.taste_item_icon);
               composites.content = (TextView) convertView.findViewById(R.id.taset_item_content);
               convertView.setTag(composites);
               TasteInfo taskInfo = datasource.get(position);
			if (taskInfo.getThumbnail_64() != null && taskInfo.getThumbnail_64().size() > 0) {
				Log.e("TQA", "v==: = " + taskInfo.getThumbnail_64());
				downImg(taskInfo.getThumbnail_64().get(0));
			}
       }else {//在convertView不为空时，Recycler根据旧的item根据数据进行置换，从而直接使用旧的item即可！
    	   composites =  (Composites) convertView.getTag();
       }
		
       
//		if(PrefsUtils.getIndex().equals("1")){
//			//加载数据
//				composites.name.setText("品酒4S法则");
//		}else{
//			//加载数据
		if(!StringUtil.isEmptyOrNull(datasource.get(position).getTitle().toString())){
			composites.name.setText(datasource.get(position).getTitle().toString() + "");
		}
//		}
		if(!StringUtil.isEmptyOrNull(datasource.get(position).getContent().toString())){
			composites.content.setText(datasource.get(position).getContent().toString().subSequence(0, 25) + "...");
		}
		if(downBItmap != null){
			composites.icon.setImageBitmap(downBItmap);
		}
       return convertView;
	}
	
	private Bitmap downBItmap;
	private void downImg(String imgurl) {
		// 启动一个后台线程
		// 这里下载数据
		try {
			URL url = new URL(imgurl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			int length = (int) conn.getContentLength();
			Log.e("TAG", "length:= " + length);
			if (length > 0) {
			if (length != -1) {
				byte[] imgData = new byte[length];
				byte[] buffer = new byte[length];
				int readLen = 0;
				int destPos = 0;
				ByteArrayOutputStream bis = new ByteArrayOutputStream(); 
				while ((readLen = is.read(buffer)) > 0) {
						bis.write(buffer, 0, readLen); 
				}
			 byte[] bb = bis.toByteArray();
			 downBItmap = BitmapFactory.decodeByteArray(bb, 0, bb.length);
			}
//			 Log.e("TAG", "downBItmap:= " + downBItmap.getWidth());
			}else{
				 
			}
			is.close();
			conn.disconnect();
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public class Composites {
		public TextView name;
		public ImageView icon;
		public TextView content;
	}
}
